#include <algorithm>
#include <cstdio>

using namespace std;

int Tree[27];
int edge_count;
struct edge {
    int a, b;
    int cost;
    bool operator<(const edge& e) const
    {
        return cost < e.cost;
    }
} edge_data[100];
int find_root(int x);

int main(int argc, char const* argv[])
{
    int n;
    while (scanf("%d", &n) != EOF && n != 0) {
        edge_count = 0;
        for (int i = 0; i < n - 1; i++) {
            char node1;
            int edge_num;
            scanf(" %c %d", &node1, &edge_num);
            for (int j = 0; j < edge_num; j++) {
                char node2;
                int cost;
                scanf(" %c %d", &node2, &cost);
                edge_data[edge_count].a = node1 - 'A';
                edge_data[edge_count].b = node2 - 'A';
                edge_data[edge_count++].cost = cost;
            }
        }
        sort(edge_data, edge_data + edge_count);
        for (int i = 0; i < n; i++)
            Tree[i] = -1;
        int result = 0;
        for (int i = 0; i < edge_count; i++) {
            int a_root = find_root(edge_data[i].a);
            int b_root = find_root(edge_data[i].b);
            if (a_root != b_root) {
                Tree[a_root] = b_root;
                result += edge_data[i].cost;
            }
        }
        printf("%d\n", result);
    }
    return 0;
}

int find_root(int x)
{
    if (Tree[x] == -1)
        return x;
    else {
        int temp = find_root(Tree[x]);
        Tree[x] = temp;
        return temp;
    }
}